package com.atguigu.day15.exer;

import org.junit.Test;

public class TextSingle {
    @Test
    public void test1() {
        SingleOne s1 = SingleOne.INSTANCE;
        SingleOne s2 = SingleOne.INSTANCE;
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    }

    @Test
    public void test2() {
        SingleTwo s1 = SingleTwo.INSTANCE;
        SingleTwo s2 = SingleTwo.INSTANCE;
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    }

    @Test
    public void test3() {
        SingleThree s1 = SingleThree.getInstance();
        SingleThree s2 = SingleThree.getInstance();
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    }

    @Test
    public void test4() {
        Runtime runtime = Runtime.getRuntime();
        System.out.println(runtime.totalMemory());
    }
}

enum SingleOne {
    INSTANCE
}

class SingleTwo {
    public static final SingleTwo INSTANCE = new SingleTwo();

    private SingleTwo() {
    }
}

class SingleThree {
    private static final SingleThree INSTANCE = new SingleThree();

    private SingleThree() {
    }

    public static SingleThree getInstance() {
        return INSTANCE;
    }
}
class SingleFour{
    private static SingleFour instance;
    private SingleFour(){}
    public static SingleFour getInstance(){
        if (instance==null){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance=new SingleFour();
        }
        return instance;
    }
}
class SingleFive{
    private static SingleFive instance;
    private SingleFive(){}
    public synchronized static SingleFive getInstance(){
        if (instance==null){
            instance=new SingleFive();
        }
        return instance;
    }
}